#include "ImageFeatureManager.h"
#include "ImageFeature.h"

GLOBAL_NAMESPACE_BEGIN

template<> ImageFeatureManager* Singleton<ImageFeatureManager>::singleton_ = 0;

ImageFeatureManager::ImageFeatureManager()
{
    nFeatureDim_ = -1;
    bDimCompatible_ = false;
}

ImageFeatureManager::~ImageFeatureManager()
{
    destroy();
}

void ImageFeatureManager::destroy()
{
    for (FeatureContainer::iterator it = features_.begin(); it!=features_.end(); it++)
    {
        if (it->second)
            delete it->second;
    }
    features_.clear();
}

void ImageFeatureManager::init()
{
}

MLAlgorithms::ImageFeature* ImageFeatureManager::addFeature(MLAlgorithms::ImageFeature* feature)
{
    if (features_.find(feature->getImage())!=features_.end())
        return NULL;

    if (features_.empty())
    {
        nFeatureDim_ = feature->getFeatureDim();
        bDimCompatible_ = true;
    }
    else if (bDimCompatible_)
    {
        bDimCompatible_ = (nFeatureDim_==feature->getFeatureDim());
    }
    else
    {
        ;
    }
    features_.insert(std::make_pair(feature->getImage(), feature));
    return feature;
}

int ImageFeatureManager::getFeatureCount()
{
    return features_.size();
}

int ImageFeatureManager::getFeatureDim()
{
    if (bDimCompatible_)
        return nFeatureDim_;
    else
        return 0;
}

MLAlgorithms::ImageFeature* ImageFeatureManager::getFeature(ImageSpace::Image* ima)
{
    FeatureIterator it = features_.find(ima);
    if (it!=features_.end())
        return it->second;
    else
        return NULL;
}

ImageFeatureManager::FeatureIterator ImageFeatureManager::featureBeginIterator()
{
    return features_.begin();
}

ImageFeatureManager::FeatureIterator ImageFeatureManager::featureEndIterator()
{
    return features_.end();
}

GLOBAL_NAMESPACE_END
